﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Sim;

namespace WindowsFormsApplication1
{
    public partial class Main : Form, IMain
    {
        public Main()
        {
            InitializeComponent();
            //tabMain.TabPages.Remove(tabPageAdmin);
            tabMain.TabPages.Remove(tabPageJudge);
            tabMain.TabPages.Remove(tabPageDiver);

        }

        private void Main_Load(object sender, EventArgs e)
        {
            this.comboBoxSortFinished.SelectedIndex = 0;
            this.comboboxSortActive.SelectedIndex = 0;
            this.comboBoxSortStatistics.SelectedIndex = 0;
            this.radioButtonDiveCode.Checked = true;
        }

        private void trackBarScore_Scroll(object sender, EventArgs e)
        {
            double x = trackBarScore.Value;
            x = x / 2;
            textBoxScore.Text = x.ToString();
        }

        private void textBoxScore_TextChanged(object sender, EventArgs e)
        {
            if (textBoxScore.Text != null)
            {
                double x = Convert.ToDouble(textBoxScore.Text);
                x = x * 2;
                trackBarScore.Value = Convert.ToInt32(x);
            }
        }

        private void radioButtonCustomDiveCode_CheckedChanged(object sender, EventArgs e)
        {
            if (textBoxDiveCode.Enabled == false)
                textBoxDiveCode.Enabled = true;
            else
                textBoxDiveCode.Enabled = false;
        }

        private void radioButtonDiveCode_CheckedChanged(object sender, EventArgs e)
        {
            if (comboBoxDiveType.Enabled == false || comboBoxDiveMotion.Enabled == false || comboBoxDiveSomersaults.Enabled == false || comboBoxDiveTwists.Enabled == false || comboBoxDivePosition.Enabled == false)
            {
                comboBoxDiveType.Enabled = true;
                comboBoxDiveMotion.Enabled = true;
                comboBoxDiveSomersaults.Enabled = true;
                comboBoxDiveTwists.Enabled = true;
                comboBoxDivePosition.Enabled = true;
            }
            else
            {
                comboBoxDiveType.Enabled = false;
                comboBoxDiveMotion.Enabled = false;
                comboBoxDiveSomersaults.Enabled = false;
                comboBoxDiveTwists.Enabled = false;
                comboBoxDivePosition.Enabled = false;
            }
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (this.EventLogin != null)
            {
                this.EventLogin(textBoxUsername.Text, textBoxPassword.Text);
                Thread.Sleep(100);
                if (Program.isLoggedIn)
                {
                    MessageBox.Show("Hej gobi gogooo");
                    tabMain.TabPages.Add(tabPageDiver);
                    groupBoxLogin.Hide();
                }
                this.EventLogin = null;
            }
        }

        private void buttonSignUp_Click(object sender, EventArgs e)
        {
            if (EventNewSignUpWindow != null)
            {
                this.EventNewSignUpWindow();
            }
        }



        public event DelegateLogin EventLogin = null;
        public event DelegateNewSignUpWindow EventNewSignUpWindow = null;
        public event DelegateNewTournament EventNewTournament = null;
        public event DelegateRefreshList EventRefreshList = null;


        private void buttonCreateTournament_Click(object sender, EventArgs e)
        {
            if (EventNewTournament != null)
            {
                Tournament t = new Tournament(textBoxTitle.Text, monthCalendar1.SelectionRange.Start.ToShortDateString(), Convert.ToInt32(comboBoxHeight.Text), comboBoxGender.Text,
                    Convert.ToInt32(textBoxMaxJudges.Text), Convert.ToInt32(textBoxMaxDivers.Text));
                EventNewTournament(t);
                MessageBox.Show("Gogoo");
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            if (EventRefreshList != null)
            {
                listView1.Items.Clear();
                this.EventRefreshList();
                Thread.Sleep(100);

                for (int i = 0; i < Program.ti.Count; i++)
                {
                    ListViewItem lvi = new ListViewItem(Program.ti[i].title);
                    lvi.SubItems.Add(Program.ti[i].date);
                    lvi.SubItems.Add(Program.ti[i].gender);
                    listView1.Items.Add(lvi);
                }
                
            }
        }
    }
}
